<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数分类id"
          :hidden="hiding"
          hasFeedback>
          <a-input placeholder="请输入参数分类id" v-decorator="['fdParamid', {'initialValue':this.mainId}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主键code"
          hasFeedback>
          <a-input placeholder="请输入主键code" v-decorator="['fdParamcode', validatorRules.fdParamcode ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="默认"
          hasFeedback>
          <a-input placeholder="请输入默认" v-decorator="['fdIddefault', validatorRules.fdIddefault ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="顺序"
          hasFeedback>
          <a-input-number v-decorator="[ 'fdOrder', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="代码描述"
          hasFeedback>
          <a-input placeholder="请输入代码描述" v-decorator="['fdParamcodedesc', validatorRules.fdParamcodedesc ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="代码名称"
          hasFeedback>
          <a-input placeholder="请输入代码名称" v-decorator="['fdParamcodename', validatorRules.fdParamcodename ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数值"
          hasFeedback>
          <a-input placeholder="请输入参数值" v-decorator="['fdParameterprice', validatorRules.fdParameterprice ]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数值类型"
          hasFeedback>
          <a-input placeholder="请输入参数值类型" v-decorator="['fdParametertype', validatorRules.fdParametertype ]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'
  import JDate from '@/components/jeecg/JDate'

  export default {
    components: {
      JDate
    },
    name: 'ParamcodeModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16}
        },
        moment,
        format: 'YYYY-MM-DD HH:mm:ss',
        disableSubmit: false,
        mainId: '',
        hiding: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          fdParamcode: { rules: [{ required: true, message: '请输入主键code!' }] },
          fdIddefault: { rules: [{ required: true, message: '请输入默认!' }] },
          fdParamcodedesc: { rules: [{ required: true, message: '请输入代码描述!' }] },
          fdParamcodename: { rules: [{ required: true, message: '请输入代码名称!' }] },
          fdParameterprice: { rules: [{ required: true, message: '请输入参数值!' }] },
          fdParametertype: { rules: [{ required: true, message: '请输入参数值类型!' }] },
        },
        url: {
          add: '/pubmgr/parameter/addParamcode',
          edit: '/pubmgr/parameter/editParamcode'
        }
      }
    },
    created() {
    },
    methods: {
      add(mainId) {
        if (mainId) {
          this.edit({mainId}, '')
        } else {
          this.$message.warning('请选择一条数据')
        }
      },
      detail(record) {
        this.edit(record, 'd')
      },
      edit(record, v) {
        if (v == 'e') {
          this.hiding = false;
          this.disableSubmit = false;
        } else if (v == 'd') {
          this.hiding = false;
          this.disableSubmit = true;
        } else {
          this.hiding = true;
          this.disableSubmit = false;
        }
        this.form.resetFields();
        this.mainId = record.mainId;
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'fdParamid', 'fdParamcode', 'fdIddefault', 'fdOrder', 'fdParamcodedesc', 'fdParamcodename', 'fdParameterprice', 'fdParametertype', 'createUser', 'updateUser', 'isDetele', ))
          // 时间格式化
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close();
      }
    }
  }
</script>

<style scoped>

</style>
